package com.atguigu.juc;

import com.atguigu.juc.AirCondition;
/**
 * @author KJ_ICU
 * @date 2021/06/22
 * 有一个初始值为零的变量，现有两个线程对该变量操作，
 * 实现一个线程对变量加1，实现一个线程对变量减1,交替来10轮
 *
 * 1 高内聚低耦合的前提下，线程       操作    资源类
 * 2 判断、干活和通知
 * 3 防止多线程的【虚假唤醒】，wait被唤醒了，需要重新进行一次判断，记得用while
 */
public class ProductComsumerDemo {
    public static void main(String[] args)
    {
        AirCondition airCondition = new AirCondition();

        new Thread(() -> {
            for (int i = 1; i <=10; i++) {
                try {
                    Thread.sleep(100);
                    airCondition.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();

        new Thread(() -> {
            for (int i = 1; i <=10; i++) {
                try {
                    Thread.sleep(200);
                    airCondition.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();

        new Thread(() -> {
            for (int i = 1; i <=10; i++) {
                try {
                    Thread.sleep(300);
                    airCondition.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();

        new Thread(() -> {
            for (int i = 1; i <=10; i++) {
                try {
                    Thread.sleep(400);
                    airCondition.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"D").start();
    }
}
